package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.CrawqueueConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.MonitorStatus;
import com.ruoyi.common.utils.RedisUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IShopeelisintinfomonitorService;
import com.ruoyi.system.service.IShopeelistinginfoService;
import com.ruoyi.system.service.IShopeeshopinfomonitorService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * shopee商品信息Controller
 * 
 * @author ruoyi
 * @date 2020-07-15
 */
@Controller
@RequestMapping("/system/shopeelistinginfo")
public class ShopeelistinginfoController extends BaseController
{
    private String prefix = "system/shopeelistinginfo";

    @Autowired
    private IShopeelistinginfoService shopeelistinginfoService;

    @Autowired
    private IShopeelisintinfomonitorService lisingmonitorService;


    @Autowired
    private IShopeeshopinfomonitorService shopeeshopinfomonitorService;

    @Autowired
    private RedisUtils redisUtils;

    @RequiresPermissions("system:shopeelistinginfo:view")
    @GetMapping()
    public String shopeelistinginfo()
    {
        return prefix + "/shopeelistinginfo";
    }

    /**
     * 查询shopee商品信息列表
     */
    @RequiresPermissions("system:shopeelistinginfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Shopeelistinginfo shopeelistinginfo)
    {
        startPage();
        List<Shopeelistinginfo> list = shopeelistinginfoService.selectShopeelistinginfoList(shopeelistinginfo);
        return getDataTable(list);
    }

    /**
     * 导出shopee商品信息列表
     */
    @RequiresPermissions("system:shopeelistinginfo:export")
    @Log(title = "shopee商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Shopeelistinginfo shopeelistinginfo)
    {
        List<Shopeelistinginfo> list = shopeelistinginfoService.selectShopeelistinginfoList(shopeelistinginfo);
        ExcelUtil<Shopeelistinginfo> util = new ExcelUtil<Shopeelistinginfo>(Shopeelistinginfo.class);
        return util.exportExcel(list, "shopeelistinginfo");
    }

    /**
     * 新增shopee商品信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存shopee商品信息
     */
    @RequiresPermissions("system:shopeelistinginfo:add")
    @Log(title = "shopee商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Shopeelistinginfo shopeelistinginfo)
    {
        return toAjax(shopeelistinginfoService.insertShopeelistinginfo(shopeelistinginfo));
    }

    /**
     * 图片展示
     */
    @GetMapping("/showimage/{itemid}")
    public String showimage(@PathVariable("itemid") String itemid, ModelMap mmap)
    {
        Shopeelistinginfo shopeelistinginfo = shopeelistinginfoService.selectShopeelistinginfoById(itemid);
        mmap.put("shopeelistinginfo", shopeelistinginfo);
        String[] imagesNames = shopeelistinginfo.getImages().replaceAll("'","").replaceAll("\\[","").replaceAll("\\]","").split(",");
        List<String> images = new ArrayList<>();
        for(String  imagename: imagesNames){
            String rooturl="https://cf.shopee.com.my/file/";
            String imageurl = rooturl+imagename.trim();
            images.add(imageurl);
        }
        mmap.put("shopeelistinginfo", shopeelistinginfo);
        mmap.put("images", images);
        return prefix + "/showimage";
    }
    /**
     * 修改shopee商品信息
     */
    @GetMapping("/edit/{itemid}")
    public String edit(@PathVariable("itemid") String itemid, ModelMap mmap)
    {
        Shopeelistinginfo shopeelistinginfo = shopeelistinginfoService.selectShopeelistinginfoById(itemid);
        mmap.put("shopeelistinginfo", shopeelistinginfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存shopee商品信息
     */
    @RequiresPermissions("system:shopeelistinginfo:edit")
    @Log(title = "shopee商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Shopeelistinginfo shopeelistinginfo)
    {
        return toAjax(shopeelistinginfoService.updateShopeelistinginfo(shopeelistinginfo));
    }


    /**
     * 产品监控
     */
    @Log(title = "产品监控", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:shopeelistinginfo:edit")
    @PostMapping("/monitorItem")
    @ResponseBody
    public AjaxResult monitorItem(ShopeeListinginfoCountry shopeelistinginfocountry)
    {
        int returnStatus=0;
        Shopeelisintinfomonitor exitsshopeelisintinfomonitor = lisingmonitorService.selectShopeelisintinfomonitorById(shopeelistinginfocountry);
        if(exitsshopeelisintinfomonitor!=null){
            if(exitsshopeelisintinfomonitor.getStatus().equals(MonitorStatus.OK.getCode())){
               return error("此商品已经在监控列表");
            }else if(exitsshopeelisintinfomonitor.getStatus().equals(MonitorStatus.DISABLE.getCode())){
                exitsshopeelisintinfomonitor.setStatus(MonitorStatus.OK.getCode());
                redisUtils.sAdd(CrawqueueConfig.getShopeeListinginfoKey(),exitsshopeelisintinfomonitor.getItemurl());
                returnStatus=lisingmonitorService.updateShopeelisintinfomonitor(exitsshopeelisintinfomonitor);
            }else{
                return error("数据错误！");
            }
        }else{
            Shopeelistinginfo shopeelistinginfo = shopeelistinginfoService.selectShopeelistinginfoById(shopeelistinginfocountry.getItemid());
            Shopeelisintinfomonitor shopeelisintinfomonitor = new Shopeelisintinfomonitor();
            shopeelisintinfomonitor.setItemid(shopeelistinginfo.getItemid());
            shopeelisintinfomonitor.setCountry(shopeelistinginfo.getCountry());
            shopeelisintinfomonitor.setShopid(shopeelistinginfo.getShopid());
            shopeelisintinfomonitor.setStatus(shopeelistinginfocountry.getStatus());
            shopeelisintinfomonitor.setItemurl(shopeelistinginfo.getItemurl());
            shopeelisintinfomonitor.setCtime(new Date());
            shopeelisintinfomonitor.setUserid(ShiroUtils.getLoginName());
            returnStatus = lisingmonitorService.insertShopeelisintinfomonitor(shopeelisintinfomonitor);
            redisUtils.sAdd(CrawqueueConfig.getShopeeListinginfoKey(),shopeelisintinfomonitor.getItemurl());
        }


        return toAjax(returnStatus);

    }
    /**
     * 店铺监控
     */
    @Log(title = "店铺监控", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:shopeelistinginfo:edit")
    @PostMapping("/monitorShop")
    @ResponseBody
    public AjaxResult monitorShop(ShopeeshopinfoCountry shopeeshopinfoCountry)
    {
        int returnStatus=0;
        Shopeeshopinfomonitor exitshopeeshopinfomonitor = shopeeshopinfomonitorService.selectShopeeshopinfomonitorByPrimary(shopeeshopinfoCountry);
        if(exitshopeeshopinfomonitor != null){
            if(exitshopeeshopinfomonitor.getStatus().equals(MonitorStatus.OK.getCode())){
                return error("此店铺已经在监控列表");
            }else if(exitshopeeshopinfomonitor.getStatus().equals(MonitorStatus.DISABLE.getCode())){
                exitshopeeshopinfomonitor.setStatus(MonitorStatus.OK.getCode());
                redisUtils.sAdd(CrawqueueConfig.getShopeeStorekey(),exitshopeeshopinfomonitor.getShopurl());
                returnStatus = shopeeshopinfomonitorService.updateShopeeshopinfomonitor(exitshopeeshopinfomonitor);
            }else{
                return error("数据错误！");
            }
        }else{
            Shopeeshopinfomonitor shopeeshopinfomonitor = new Shopeeshopinfomonitor();
            shopeeshopinfomonitor.setShopid(shopeeshopinfoCountry.getShopid());
            shopeeshopinfomonitor.setUserid(ShiroUtils.getLoginName());
            shopeeshopinfomonitor.setCtime(new Date());
            shopeeshopinfomonitor.setStatus(MonitorStatus.OK.getCode());
            shopeeshopinfomonitor.setCountry(shopeeshopinfoCountry.getCountry());
            shopeeshopinfomonitor.setShopurl(String.format("https://my.xiapibuy.com/api/v4/shop/get_shop_detail?shopid=%s&username=",shopeeshopinfoCountry.getShopid() ));
            returnStatus = shopeeshopinfomonitorService.insertShopeeshopinfomonitor(shopeeshopinfomonitor);
            redisUtils.sAdd(CrawqueueConfig.getShopeeStorekey(),shopeeshopinfomonitor.getShopurl());

        }
        return toAjax(returnStatus);

    }
    /**
     * 删除shopee商品信息
     */
    @RequiresPermissions("system:shopeelistinginfo:remove")
    @Log(title = "shopee商品信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shopeelistinginfoService.deleteShopeelistinginfoByIds(ids));
    }
}
